from motor.resources.instance import Instance
from motor.utils.http_client import SafeHTTPSClient
from motor.utils.logger import get_logger

logger = get_logger(__name__)

class NodeManagerApiClient:

    def start(self):
        pass


    def stop(self, ins: Instance):
        is_succeed = True
        for node_mgr in ins.get_node_managers():
            base_url = f"http://{node_mgr.pod_ip}:{node_mgr.port}"
            try:
                client = SafeHTTPSClient(base_url)
                response = client.post("/nodemanager/stop", data={})
                logger.info(f"Stop command sent to node manager {node_mgr.pod_ip}:{node_mgr.port} "
                            f"for instance {ins.job_name} successfully.")
            except Exception as e:
                is_succeed = False
                logger.error(f"Error sending stop command to node manager {node_mgr.pod_ip}:{node_mgr.port} "
                             f"for instance {ins.job_name}: {e}")
            finally:
                client.close()

        return is_succeed